package com.shyroke.calculation;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import entity.AttributeWeightDto;
import entity.ConvertExpert;

/**
 * 计算属性权重
 * @author shyroke
 *
 */
public class AttributeWeightCal {
	

	/**
	 * 计算E（A）
	 * @param convertExpertList
	 * @return 最外层map 的key代表是第几个专家，内层map的key代表第几个属性,比如<1,<c2,0.276>>  表示第一个专家的第二个属性的E（A）值为0.276
	 */
	public static Map<Integer,Map<String,Double>> cal_E(List<ConvertExpert> convertExpertList) {
		Map<Integer,Map<String,Double>> resultMap ;
		if(convertExpertList.size()>0) {
			resultMap = new HashMap<>();
			
			
			for(ConvertExpert expert : convertExpertList) {
				/*
				 * 计算每个专家每个属性的E(A)
				 */
				Map<String,Double> attriMap = new HashMap<>();
				
				//计算第一个属性的E(A)
				List<Double> zzj_c1 = expert.getS11Map().get("s11");
				double attri_EA_1 = (zzj_c1.get(0)+zzj_c1.get(2)) * Math.abs(zzj_c1.get(1) - (1 - zzj_c1.get(1)));
				
				List<Double> zzj_c2 = expert.getS21Map().get("s21");
				double attri_EA_2 = (zzj_c2.get(0)+zzj_c2.get(2)) * Math.abs(zzj_c2.get(1) - (1 - zzj_c2.get(1)));
				
				List<Double> zzj_c3 = expert.getS31Map().get("s31");
				double attri_EA_3 = (zzj_c3.get(0)+zzj_c3.get(2)) * Math.abs(zzj_c3.get(1) - (1 - zzj_c3.get(1)));
				
				double allPlu = attri_EA_1 + attri_EA_2 + attri_EA_3;
				
				double Ea = 1 - Double.valueOf(new DecimalFormat("#.0000").format(  (allPlu / 3)  ));
				attriMap.put("c1", 1-Ea);
//				System.out.println("Ea=========================="+Ea);
				
				//计算第二个属性的E（A）
				List<Double> zzj_c1_c2 = expert.getS12Map().get("s12");
				double attri_EA_1_c2 = (zzj_c1_c2.get(0)+zzj_c1_c2.get(2)) * Math.abs(zzj_c1_c2.get(1) - (1 - zzj_c1_c2.get(1)));
				
				List<Double> zzj_c2_c2 = expert.getS22Map().get("s22");
				double attri_EA_2_c2 = (zzj_c2_c2.get(0)+zzj_c2_c2.get(2)) * Math.abs(zzj_c2_c2.get(1) - (1 - zzj_c2_c2.get(1)));
				
				List<Double> zzj_c3_c2 = expert.getS32Map().get("s32");
				double attri_EA_c2 = (zzj_c3_c2.get(0)+zzj_c3_c2.get(2)) * Math.abs(zzj_c3_c2.get(1) - (1 - zzj_c3_c2.get(1)));
				
				double allPlu2 = attri_EA_1_c2 + attri_EA_2_c2 + attri_EA_c2;
				
				double Ea_c2 = 1 - Double.valueOf(new DecimalFormat("#.0000").format(  (allPlu2 / 3)  ));
				attriMap.put("c2", 1-Ea_c2);
				
				//计算第三个属性的E(A)
				List<Double> zzj_c1_c3 = expert.getS13Map().get("s13");
				double attri_EA_1_c3 = (zzj_c1_c3.get(0)+zzj_c1_c3.get(2)) * Math.abs(zzj_c1_c3.get(1) - (1 - zzj_c1_c3.get(1)));
				
				List<Double> zzj_c2_c3 = expert.getS23Map().get("s23");
				double attri_EA_2_c3 = (zzj_c2_c3.get(0)+zzj_c2_c3.get(2)) * Math.abs(zzj_c2_c3.get(1) - (1 - zzj_c2_c3.get(1)));
				
				List<Double> zzj_c3_c3 = expert.getS33Map().get("s33");
				double attri_EA_c3 = (zzj_c3_c3.get(0)+zzj_c3_c3.get(2)) * Math.abs(zzj_c3_c3.get(1) - (1 - zzj_c3_c3.get(1)));
				
				double allPlu3 = attri_EA_1_c3 + attri_EA_2_c3 + attri_EA_c3;
				
				double Ea_c3 = 1 - Double.valueOf(new DecimalFormat("#.0000").format(  (allPlu3 / 3)  ));
				attriMap.put("c3", 1-Ea_c3);
				
				
				//第四个属性的E(A)
				List<Double> zzj_c1_c4 = expert.getS14Map().get("s14");
				double attri_EA_1_c4 = (zzj_c1_c4.get(0)+zzj_c1_c4.get(2)) * Math.abs(zzj_c1_c4.get(1) - (1 - zzj_c1_c4.get(1)));
				
				List<Double> zzj_c2_c4 = expert.getS24Map().get("s24");
				double attri_EA_2_c4 = (zzj_c2_c4.get(0)+zzj_c2_c4.get(2)) * Math.abs(zzj_c2_c4.get(1) - (1 - zzj_c2_c4.get(1)));
				
				List<Double> zzj_c3_c4 = expert.getS34Map().get("s34");
				double attri_EA_c4 = (zzj_c3_c4.get(0)+zzj_c3_c4.get(2)) * Math.abs(zzj_c3_c4.get(1) - (1 - zzj_c3_c4.get(1)));
				
				double allPlu4 = attri_EA_1_c4 + attri_EA_2_c4 + attri_EA_c4;
				
				double Ea_c4 = 1 - Double.valueOf(new DecimalFormat("#.0000").format(  (allPlu4 / 3)  ));
				attriMap.put("c4", 1-Ea_c4);
				
				resultMap.put(expert.getId(), attriMap);
			}

			
			
			return resultMap;
		}else {
			throw new RuntimeException("专家中智集集合不能为空");
		}
		
	}
	
	
	/**
	 * 获取属性权重
	 * @return key代表第几个属性，value代表属性权重值
	 */
	public static Map<String,Double> getAttributeWeight(List<ConvertExpert> convertExpertList){
		Map<String,Double> resultMap = new HashMap<>();
		
		Map<Integer,Map<String,Double>> getEA = cal_E(convertExpertList);
		 //TODO 1-E(A) 还没算
		
		return resultMap;
	}


	/**
	 * 计算属性权重
	 * @param ea
	 * @return key表示第几个属性，value表示权重值
	 */
	public static Map<String, Double> calAttributeWeight(List<ConvertExpert> convertExpertList,Map<String,Double> expertImportance) {
		Map<String, Double> resultMap = new HashMap<>();
		Map<Integer,Map<String,Double>> Ea =AttributeWeightCal.cal_E(convertExpertList);
		/**
		 * 获取专家的每个属性的权重值
		 */
		List<AttributeWeightDto> attributeWeightDtos = attributeWeightByExpert(Ea);
		
		for(int i=1;i<=expertImportance.size();i++) {
			double attributeWeightValue = 0;
			List<Double> c_weight = new ArrayList<>();
			for(int j=1;j<=attributeWeightDtos.size();j++) {
				if(i==1) {
					c_weight.add(attributeWeightDtos.get(j-1).getC1());
				}else if(i==2) {
					c_weight.add(attributeWeightDtos.get(j-1).getC2());
				}else if(i==3) {
					c_weight.add(attributeWeightDtos.get(j-1).getC3());
				}else if(i==4) {
					c_weight.add(attributeWeightDtos.get(j-1).getC4());
				}
			}
			for(int j=1;j<=c_weight.size();j++) {
				attributeWeightValue =Double.valueOf(new DecimalFormat("#.0000").format( attributeWeightValue + expertImportance.get(String.valueOf(j))*c_weight.get(j-1)));
			}
			
			resultMap.put("c"+i, attributeWeightValue);
		}
		
		return resultMap;
	}


	/**
	 * 计算专家的每个属性的权重值
	 * @param ea
	 * @return
	 */
	private static List<AttributeWeightDto> attributeWeightByExpert(Map<Integer, Map<String, Double>> ea) {
		List<AttributeWeightDto> attributeWeightDtos =new ArrayList<>();
		//key表示专家数，value表示1-E的和
		Map<Integer,Double> ETotalMap = new HashMap<>();
		for(int i=1; i<=ea.size() ; i++) {
			double etotal = 0;
			Map<String,Double> Ea = ea.get(i);
			for(int j=1; j<= Ea.size(); j++) {
				double c_ea = Ea.get("c"+j);
				etotal = c_ea+etotal;
			}
			ETotalMap.put(i, etotal);
		}
		
		for(int i=1; i<=ea.size() ; i++) {
			AttributeWeightDto attributeWeightDto=new AttributeWeightDto();
			attributeWeightDto.setId(i);
			double etotal = ETotalMap.get(i);
			Map<String,Double> Ea = ea.get(i);
			for(int j=1; j<= Ea.size(); j++) {
				double c_weight = Double.valueOf(new DecimalFormat("#.0000").format(Ea.get("c"+j)/etotal));
				
				if(j==1) {
					attributeWeightDto.setC1(c_weight);
				}else if(j==2) {
					attributeWeightDto.setC2(c_weight);
				}else if(j==3) {
					attributeWeightDto.setC3(c_weight);
				}else if(j==4) {
					attributeWeightDto.setC4(c_weight);
				}
				
			}
			attributeWeightDtos.add(attributeWeightDto);
		}
		
		return attributeWeightDtos;
	}
	
	
	
	
}
